| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205 |
1×
1×
1×
| //
//
//
//
//
//
//
//
import {getWidth} from '../utils'
import keycode from 'keycode'
import clickoutside from '../internal/clickoutside'
export default {
name: 'mu-menu',
props: {
desktop: {
type: Boolean,
default: false
},
multiple: {
type: Boolean,
default: false
},
autoWidth: {
type: Boolean,
default: true
},
width: {
type: [String, Number]
},
maxHeight: {
type: Number
},
disableAutoFocus: {
type: Boolean,
default: false
},
initiallyKeyboardFocused: {
type: Boolean,
default: false
},
// 内部使用,是否是弹出菜单
popover: {
type: Boolean,
default: false
},
value: {}
},
data () {
return {
focusIndex: -1,
isKeyboardFocused: false
}
},
computed: {
keyWidth () {
return this.desktop ? 64 : 56
},
contentWidth () {
return this.autoWidth ? '' : getWidth(this.width)
}
},
mounted () {
this.setWidth()
const selectedIndex = this.getSelectedIndex()
this.setScollPosition()
this.focusIndex = this.disableAutoFocus ? -1 : selectedIndex >= 0 ? selectedIndex : this.initiallyKeyboardFocused ? 0 : -1
this.isKeyboardFocused = this.initiallyKeyboardFocused
if (this.popover) this.$el.focus()
},
beforeUpdate () {
const selectedIndex = this.getSelectedIndex()
this.focusIndex = this.disableAutoFocus ? -1 : selectedIndex >= 0 ? selectedIndex : 0
},
updated () {
this.setWidth()
},
methods: {
clickoutside () {
this.setFocusIndex(-1, false)
},
setWidth () {
if (!this.autoWidth) return
const el = this.$el
const listEl = this.$refs.list
const elWidth = el.offsetWidth
if (elWidth === 0) return // 被隐藏时不计算宽度
const keyWidth = this.keyWidth
const minWidth = keyWidth * 1.5
let keyIncrements = elWidth / keyWidth
let newWidth
keyIncrements = keyIncrements <= 1.5 ? 1.5 : Math.ceil(keyIncrements)
newWidth = keyIncrements * keyWidth
if (newWidth < minWidth) newWidth = minWidth
el.style.width = `${newWidth}px`
listEl.style.width = `${newWidth}px`
},
handleChange (value) {
this.$emit('change', value)
},
handleClick (item) {
this.$emit('itemClick', item)
},
handleKeydown (event) {
const key = keycode(event)
switch (key) {
case 'down':
event.stopPropagation() // 防止菜单嵌套
event.preventDefault()
this.incrementKeyboardFocusIndex()
break
case 'tab':
event.stopPropagation()
event.preventDefault()
if (event.shiftKey) {
this.decrementKeyboardFocusIndex()
} else {
this.incrementKeyboardFocusIndex()
}
break
case 'up':
event.stopPropagation()
event.preventDefault()
this.decrementKeyboardFocusIndex()
break
}
},
decrementKeyboardFocusIndex () {
let index = this.focusIndex
const maxIndex = this.getMenuItemCount() - 1
index--
if (index < 0) index = maxIndex
this.setFocusIndex(index, true)
},
incrementKeyboardFocusIndex () {
let index = this.focusIndex
const maxIndex = this.getMenuItemCount() - 1
index++
if (index > maxIndex) index = 0
this.setFocusIndex(index, true)
},
getMenuItemCount () {
let menuItemCount = 0
this.$children.forEach((child) => {
if (child._isMenuItem && !child.disabled) menuItemCount++
})
return menuItemCount
},
getSelectedIndex () {
let selectedIndex = -1
let menuItemIndex = 0
this.$children.forEach((child) => {
if (child.active) selectedIndex = menuItemIndex
if (child._isMenuItem && !child.disabled) menuItemIndex++
})
return selectedIndex
},
setFocusIndex (newIndex, isKeyboardFocused) {
this.focusIndex = newIndex
this.isKeyboardFocused = isKeyboardFocused
},
setScollPosition (selectedIndex) {
const desktop = this.desktop
let focusedMenuItem = null
this.$children.forEach((child) => {
if (child.active) focusedMenuItem = child
})
const menuItemHeight = desktop ? 32 : 48
if (focusedMenuItem) {
const selectedOffSet = focusedMenuItem.$el.offsetTop
// Make the focused item be the 2nd item in the list the user sees
let scrollTop = selectedOffSet - menuItemHeight
if (scrollTop < menuItemHeight) scrollTop = 0
this.$refs.list.scrollTop = scrollTop
}
}
},
watch: {
focusIndex (newVal, oldVal) {
if (newVal === oldVal) return
let menuItemIndex = 0
this.$children.forEach((child) => {
if (!child._isMenuItem || child.disabled) return
const isFocused = menuItemIndex === newVal
let focusState = 'none'
if (isFocused) {
focusState = this.isKeyboardFocused ? 'keyboard-focused' : 'focused'
}
child.focusState = focusState
menuItemIndex++
})
}
},
directives: {
clickoutside
}
}
|